/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.devs.benchmark.modeling;

import java.util.List;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "connectionType", propOrder = {})
/**
 *
 * @author Alejandro Moreno
 */
public class ConnectionType {

    @XmlTransient
    ModelType objectModelTo;

    @XmlTransient
    ModelType objectModelFrom;

    @XmlTransient
    PortType objectPortTo;

    @XmlTransient
    PortType objectPortFrom;

    @XmlAttribute(name = "portTo")
    String portTo = "";
    @XmlAttribute(name = "modelTo")
    String modelTo = "";
    @XmlAttribute(name = "portFrom")
    String portFrom = "";
    @XmlAttribute(name = "modelFrom")
    String modelFrom = "";

    public ConnectionType(){}

    /*public ConnectionType(String modelFrom, String portFrom, String modelTo, String portTo){
        this.modelFrom = modelFrom;
        this.portFrom = portFrom;
        this.modelTo = modelTo;
        this.portTo = portTo;
    }*/
     public ConnectionType(ModelType modelFrom, PortType portFrom, ModelType modelTo, PortType portTo){
        this.objectModelFrom = modelFrom;
        this.objectPortFrom = portFrom;
        this.objectModelTo = modelTo;
        this.objectPortTo = portTo;

        this.modelFrom = objectModelFrom.getName();
        this.portFrom = objectPortFrom.getName();
        this.modelTo = objectModelTo.getName();
        this.portTo = objectPortTo.getName();
        
    }


    public String getModelFrom() {
        return modelFrom;
    }

    public void setModelFrom(String modelFrom) {
        this.modelFrom = modelFrom;
    }

    public String getModelTo() {
        return modelTo;
    }

    public void setModelTo(String modelTo) {
        this.modelTo = modelTo;
    }

    public String getPortFrom() {
        return portFrom;
    }

    public void setPortFrom(String portFrom) {
        this.portFrom = portFrom;
    }

    public String getPortTo() {
        return portTo;
    }

    public void setPortTo(String portTo) {
        this.portTo = portTo;
    }

    public void afterUnmarshal(Unmarshaller u, Object parent) {
        this.objectModelFrom = getModel(this.modelFrom,((CoupledType)parent).getModels());
        this.objectModelTo = getModel(this.modelTo,((CoupledType)parent).getModels());
        this.objectPortTo = getInPort(this.portFrom,this.objectModelFrom.getInports());
        this.objectPortFrom = getOutPort(this.portFrom,this.objectModelFrom.getOutports());
    }


    public ModelType getModel(String name, List<ModelType> models){
        for(int i=0;i<models.size();i++ )
            if(models.get(i).getName().equalsIgnoreCase(name))
                return models.get(i);
        return null;
    }

    public PortType getInPort(String name, List<InportType> ports){
        for(int i=0;i<ports.size();i++ )
            if(ports.get(i).getName().equalsIgnoreCase(name))
                return ports.get(i);
        return null;
    }

    public PortType getOutPort(String name, List<OutportType> ports){
        for(int i=0;i<ports.size();i++ )
            if(ports.get(i).getName().equalsIgnoreCase(name))
                return ports.get(i);
        return null;
    }


    public ModelType getObjectModelFrom() {
        return objectModelFrom;
    }

    public void setObjectModelFrom(ModelType objectModelFrom) {
        this.objectModelFrom = objectModelFrom;
    }

    public ModelType getObjectModelTo() {
        return objectModelTo;
    }

    public void setObjectModelTo(ModelType objectModelTo) {
        this.objectModelTo = objectModelTo;
    }

    public PortType getObjectPortFrom() {
        return objectPortFrom;
    }

    public void setObjectPortFrom(OutportType objectPortFrom) {
        this.objectPortFrom = objectPortFrom;
    }

    public PortType getObjectPortTo() {
        return objectPortTo;
    }

    public void setObjectPortTo(InportType objectPortTo) {
        this.objectPortTo = objectPortTo;
    }



}
